package com.shxc.smarttrade.old.activity;

import android.support.v4.app.ActivityCompat;

import java.lang.ref.WeakReference;

import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;

/**
 * Created by jcdev1 on 2017/7/6.
 */

final class WebViewActivityPermissionsDispatchers {
    private static final int REQUEST_GETIMGFROMCAMERA = 0;

    private static final String[] PERMISSION_GETIMGFROMCAMERA = new String[] {"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE"};

    private static final int REQUEST_GETIMGFROMALBUM = 1;

    private static final String[] PERMISSION_GETIMGFROMALBUM = new String[] {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"};

    private WebViewActivityPermissionsDispatchers() {
    }

    static void getImgFromCameraWithCheck(WebViewActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_GETIMGFROMCAMERA)) {
            target.getImgFromCamera();
        } else {
            if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_GETIMGFROMCAMERA)) {
                target.showRationale(new GetImgFromCameraPermissionRequest(target));
            } else {
                ActivityCompat.requestPermissions(target, PERMISSION_GETIMGFROMCAMERA, REQUEST_GETIMGFROMCAMERA);
            }
        }
    }

    static void getImgFromAlbumWithCheck(WebViewActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_GETIMGFROMALBUM)) {
            target.getImgFromAlbum();
        } else {
            if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_GETIMGFROMALBUM)) {
                target.noSdCardPermission(new GetImgFromAlbumPermissionRequest(target));
            } else {
                ActivityCompat.requestPermissions(target, PERMISSION_GETIMGFROMALBUM, REQUEST_GETIMGFROMALBUM);
            }
        }
    }

    static void onRequestPermissionsResult(WebViewActivity target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_GETIMGFROMCAMERA:
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.getImgFromCamera();
                } else {
                    if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_GETIMGFROMCAMERA)) {
                        target.showNeverAsk();
                    } else {
                        target.showDenied();
                    }
                }
                break;
            case REQUEST_GETIMGFROMALBUM:
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.getImgFromAlbum();
                } else {
                    target.showStorageDenied();
                }
                break;
            default:
                break;
        }
    }

    private static final class GetImgFromCameraPermissionRequest implements PermissionRequest {
        private final WeakReference<WebViewActivity> weakTarget;

        private GetImgFromCameraPermissionRequest(WebViewActivity target) {
            this.weakTarget = new WeakReference<WebViewActivity>(target);
        }

        @Override
        public void proceed() {
            WebViewActivity target = weakTarget.get();
            if (target == null) return;
            ActivityCompat.requestPermissions(target, PERMISSION_GETIMGFROMCAMERA, REQUEST_GETIMGFROMCAMERA);
        }

        @Override
        public void cancel() {
            WebViewActivity target = weakTarget.get();
            if (target == null) return;
            target.showDenied();
        }
    }

    private static final class GetImgFromAlbumPermissionRequest implements PermissionRequest {
        private final WeakReference<WebViewActivity> weakTarget;

        private GetImgFromAlbumPermissionRequest(WebViewActivity target) {
            this.weakTarget = new WeakReference<WebViewActivity>(target);
        }

        @Override
        public void proceed() {
            WebViewActivity target = weakTarget.get();
            if (target == null) return;
            ActivityCompat.requestPermissions(target, PERMISSION_GETIMGFROMALBUM, REQUEST_GETIMGFROMALBUM);
        }

        @Override
        public void cancel() {
            WebViewActivity target = weakTarget.get();
            if (target == null) return;
            target.showStorageDenied();
        }
    }
}

